package com.hzcx.app.simplechat.ui.publicui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.amap.api.location.AMapLocation;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.ui.chat.bean.GdMapPoiBean;
import com.hzcx.app.simplechat.ui.publicui.adapter.LocationAdapter;
import com.hzcx.app.simplechat.ui.publicui.event.LocationEvent;
import com.hzcx.app.simplechat.util.log.LogUtils;
import com.hzcx.app.simplechat.util.map.GdMapUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 附近地址页面
 */
public class LocationActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.cons_search)
    ConstraintLayout consSearch;
    @BindView(R.id.rv_location)
    RecyclerView rvLocation;
    @BindView(R.id.tv_hide_address)
    TextView tvHideAddress;
    @BindView(R.id.ll_main)
    LinearLayout llMain;

    private List<GdMapPoiBean> list;
    private LocationAdapter locationAdapter;
    private String city;

    @Override
    public int getLayout() {
        return R.layout.activity_location;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        tvTitle.setText("位置");
        list = new ArrayList<>();
        locationAdapter = new LocationAdapter(list);
        rvLocation.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvLocation.setAdapter(locationAdapter);
    }

    @Override
    public void initData() {
        showLoading();
        GdMapUtils.getInstance().startLocation(aMapLocation -> {
            if (aMapLocation.getErrorCode() != 0) {
                LogUtils.d("定位失败");
                return;
            }
            LogUtils.d("定位地址：" + aMapLocation.getAddress());
            searchAddress(aMapLocation);
            city = aMapLocation.getCity();
        });

        locationAdapter.setOnItemClickListener((adapter, view, position) -> {
            EventBus.getDefault().post(new LocationEvent(list.get(position).getLat(), list.get(position).getLng(), city+"·"+list.get(position).getTitle()));
        });
    }

    @OnClick({R.id.tv_hide_address, R.id.cons_search})
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_hide_address:
                EventBus.getDefault().post(new LocationEvent(0, 0, ""));
                break;

            case R.id.cons_search:
                startActivity(new Intent(this, LocationSearchActivity.class)
                        .putExtra(LocationSearchActivity.INTENT_CITY, city));
                break;
        }
    }

    private void searchAddress(AMapLocation location) {
        GdMapUtils.getInstance().getAddressList(location, new PoiSearch.OnPoiSearchListener() {
            @Override
            public void onPoiSearched(PoiResult poiResult, int i) {
                if (i == 1000) {
                    if (poiResult != null && poiResult.getQuery() != null) {
                        if (poiResult.getQuery().equals(GdMapUtils.getInstance().getQuery())) {
                            for (PoiItem item : poiResult.getPois()) {
                                GdMapPoiBean bean = new GdMapPoiBean();
                                bean.setLat(item.getLatLonPoint().getLatitude());
                                bean.setLng(item.getLatLonPoint().getLongitude());
                                bean.setTitle(item.getTitle());
                                bean.setAddress(item.getAdName() + item.getSnippet());
                                list.add(bean);
                            }
                            locationAdapter.notifyDataSetChanged();
                            hideLoading();
                            llMain.setVisibility(View.VISIBLE);
                        }
                    }
                }
            }

            @Override
            public void onPoiItemSearched(PoiItem poiItem, int i) {

            }
        });
    }

    /**
     * 地址选择
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void selectLocation(LocationEvent event) {
        finish();
    }

}
